package com.gst.handfood.controller.activity;

import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.gst.handfood.R;
import com.gst.handfood.base.BaseActivity;
import com.gst.handfood.controller.fragment.HomeFragment;
import com.gst.handfood.controller.fragment.LoveFragment;
import com.gst.handfood.controller.fragment.MarketFragment;
import com.gst.handfood.controller.fragment.MessFragment;
import com.gst.handfood.controller.fragment.SelfFragment;

import butterknife.Bind;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {
    @Bind(R.id.fl)
    FrameLayout fl;
    @Bind(R.id.rb_home)
    RadioButton rbHome;
    @Bind(R.id.rb_market)
    RadioButton rbMarket;
    @Bind(R.id.rb_love)
    RadioButton rbLove;
    @Bind(R.id.rb_mess)
    RadioButton rbMess;
    @Bind(R.id.rb_self)
    RadioButton rbSelf;
    @Bind(R.id.rg_main)
    RadioGroup rgMain;

    private HomeFragment homeFragment;
    private MarketFragment marketFragment;
    private LoveFragment loveFragment;
    private MessFragment messFragment;
    private SelfFragment selfFragment;
    private FragmentTransaction ft;


    @Override
    protected void initData() {
        rgMain.check(R.id.rb_home);
        setSelect(0);
    }

    @Override
    protected void initTitle() {
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @OnClick({R.id.rb_home, R.id.rb_market, R.id.rb_love, R.id.rb_mess, R.id.rb_self})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_home:
                setSelect(0);
                break;
            case R.id.rb_market:
                setSelect(1);
                break;
            case R.id.rb_love:
                setSelect(2);
                break;
            case R.id.rb_mess:
                setSelect(3);
                break;
            case R.id.rb_self:
                setSelect(4);
                break;
        }
    }

    private void setSelect(int i) {
        ft = getSupportFragmentManager().beginTransaction();
        //每点一次都隐藏一次
        hideFragment();
        switch (i) {
            case 0:
                //首页
                if (homeFragment==null) {
                    homeFragment = new HomeFragment();
                    ft.add(R.id.fl,homeFragment);
                }

                ft.show(homeFragment);
                break;
            case 1:
                //市集
                if (marketFragment == null) {
                    marketFragment = new MarketFragment();
                    ft.add(R.id.fl, marketFragment);
                }
                ft.show(marketFragment);
                break;
            case 2:
                //收藏
                if (loveFragment == null) {
                    loveFragment = new LoveFragment();
                    ft.add(R.id.fl, loveFragment);
                }
                ft.show(loveFragment);
                break;
            case 3:
                //消息
                if (messFragment == null) {
                    messFragment = new MessFragment();
                    ft.add(R.id.fl, messFragment);
                }
                ft.show(messFragment);
                break;
            case 4:
                //我
                if (selfFragment == null) {
                    selfFragment = new SelfFragment();
                    ft.add(R.id.fl, selfFragment);
                }
                ft.show(selfFragment);
                break;
        }
        ft.commit();
    }
    private void hideFragment() {
        if (homeFragment != null) {
            ft.hide(homeFragment);
        }
        if (marketFragment != null) {
            ft.hide(marketFragment);
        }
        if (loveFragment != null) {
            ft.hide(loveFragment);
        }
        if (messFragment != null) {
            ft.hide(messFragment);
        }if (selfFragment != null) {
            ft.hide(selfFragment);
        }
    }
}
